package com.baosight.risk.service.jlta.impl;

import com.baosight.risk.api.jlta.JltaTaskService;
import com.baosight.risk.base.service.BaseService;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.jlta.JltaTaskMapper;
import com.baosight.risk.mapper.jlta.JltaTeamUserMapper;
import com.baosight.risk.service.jlta.entity.JltaTask;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.jlta.vo.JltaTaskVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月08日
 * 描述： JltaTaskServiceImpl
 * 版本: v1.0
 */
@Service
@Transactional
public class JltaTaskServiceImpl extends BaseServiceImpl<JltaTask> implements JltaTaskService {

    @Autowired
    private JltaTaskMapper jltaTaskMapper;
    @Autowired
    private JltaTeamUserMapper jltaTeamUserMapper;

    @Override
    public List<JltaTask> selectTaskList(JltaTask jltaTask) {
        XtzyUser user = ShiroUtils.getUser();
        String userCode = user.getUserCode();
        List<JltaTask> jltaTasks = new ArrayList<>();
        String teamId = jltaTask.getTeamId();
        if (teamId != null) {
            //角色查询
            String role = getRole(userCode, jltaTask.getTeamId());
            if ("0".equals(role)) {
                jltaTask.setCreateBy(userCode);
            }
            servicePage();
            jltaTasks = jltaTaskMapper.selectTaskList(jltaTask);
            return jltaTasks;
        }
        return jltaTasks;
    }

    @Override
    public List<JltaTaskVO> selectFileList(String id) {
        return jltaTaskMapper.selectFileList(id);
    }

    @Override
    public Integer addTask(JltaTask jltaTask) {
        String taskCode = "";
        String s = jltaTaskMapper.selectMaxTaskCodeByTeamId(jltaTask.getTeamId());
        if (s == null) {
            taskCode = "1";
        } else {
            taskCode = "" + (Integer.parseInt(s) + 1);
        }
        jltaTask.setTaskCode(taskCode);
        return jltaTaskMapper.insert(jltaTask);
    }

    @Override
    public Integer editTask(JltaTask jltaTask) {
        return jltaTaskMapper.updateByPrimaryKeySelective(jltaTask);
    }

    @Override
    public Integer deleteTask(JltaTask jltaTask) {
        return jltaTaskMapper.deleteTask(jltaTask);
    }

    @Override
    public JltaTask getTasksByIds(String id) {
        return jltaTaskMapper.getTasksByIds(id);
    }

    private String getRole(String userCode, String teamId) {
        JltaTeamUser teamUserQ = new JltaTeamUser();
        teamUserQ.setUserCode(userCode);
        teamUserQ.setTeamId(teamId);
        JltaTeamUser teamUser = jltaTeamUserMapper.getByUserCodeAndTeamId(teamUserQ);
        String role = teamUser.getRole();
        return role;

    }
}
